/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        ListNode guard(-1);
        guard.next=head;
        head=&guard;
        for (int i=0;i<m-1;i++) head=head->next;
        ListNode *cur=head->next, *tail=cur;
        for (int i=m;i<=n;i++) {
            ListNode *tmp=cur->next;
            cur->next=head->next;
            head->next=cur;
            cur=tmp;
        }
        tail->next=cur;
        return guard.next;
    }
};
